# coding:utf-8

import re
# Python 自1.5版本起增加了re 模块

# 7
# re.A # ASCII
# re.I # IGNORECASE
# re.L # LOCALE
# re.M # MULTILINE
# re.S # DOTALL
# re.X # VERBOSE
# re.U # UNICODE


# match(pattern, string, flags=0) -> Match / None
# fullmatch(pattern, string, flags=0)
# search(pattern, string, flags=0) -> Match / None
# sub(pattern, repl, string, count=0, flags=0)
# subn(pattern, repl, string, count=0, flags=0)
# split(pattern, string, maxsplit=0, flags=0)
# findall(pattern, string, flags=0)
# finditer(pattern, string, flags=0)
# compile(pattern, flags=0) -> Pattern
# purge()
# template(pattern, flags=0)
# escape(pattern)

# 01 从头匹配一个符合规则的字符串，从起始位置开始匹配，匹配成功返回一个对象，未匹配成功返回None。
# match(pattern, string, flags=0) -> Math / None
match = re.match('www', 'www.baidu.com')
print(type(match))  # <class '_sre.SRE_Match'>
print(match)  # <_sre.SRE_Match object; span=(0, 3), match='www'>
print(type(match.string), match.string)  # <class 'str'> www.baidu.com
print(type(match.re), match.re)  # <class '_sre.SRE_Pattern'>   re.compile('www')
print(type(match.endpos), match.endpos)  # <class 'int'> 13
print(type(match.regs), match.regs)  # <class 'tuple'> ((0, 3),)

print(type(match.span()), match.span())  # <class 'tuple'>  (0, 3) #返回一个元组包含匹配 (开始,结束) 的位置
print(type(match.start()), match.start())  # <class 'int'> 0       # 返回匹配开始的位置
print(type(match.end()), match.end())  # <class 'int'> 3           # 返回匹配结束的位置
print(type(match.group()), match.group())  # <class 'str'> www     # 返回被 RE 匹配的整个表达式的字符串，group() 可以一次输入多个组号，在这种情况下它将返回一个包含那些组所对应值的元组
print(type(match.groups()), match.groups())  # <class 'tuple'> ()  # 返回一个包含所有小组字符串的元组，从 1 到 所含的小组号
print(type(match.groupdict()), match.groupdict())  # <class 'dict'> {}

print('---------------------')
print('test example')
line = "Cats are smarter than dogs"
# TODO: why not return group(3) 只返回括号里面的
matchObj = re.match(r'(.*) are (.*?) .*', line, re.M | re.I)
if matchObj:
    print('matchObj: ', matchObj)
    print("matchObj.group() : ", type(matchObj.group()), matchObj.group())
    print("matchObj.group(0) : ", matchObj.group(0))
    print("matchObj.group(1) : ", matchObj.group(1))
    print("matchObj.group(2) : ", type(matchObj.group(2)), matchObj.group(2))
    print('matchObj.groups():', type(matchObj.groups()), matchObj.groups())
    print('matchObj.groups(0):', type(matchObj.groups(0)), matchObj.groups(0))
    print('matchObj.groups(1):', type(matchObj.groups(1)), matchObj.groups(1))
    print('matchObj.groups(2):', type(matchObj.groups(2)), matchObj.groups(2))
    # matchObj:  <_sre.SRE_Match object; span=(0, 26), match='Cats are smarter than dogs'>
    # matchObj.group() :  <class 'str'> Cats are smarter than dogs
    # matchObj.group(0) :  Cats are smarter than dogs
    # matchObj.group(1) :  Cats
    # matchObj.group(2) :  <class 'str'> smarter
    # matchObj.groups(): <class 'tuple'> ('Cats', 'smarter')
    # matchObj.groups(0): <class 'tuple'> ('Cats', 'smarter')
    # matchObj.groups(1): <class 'tuple'> ('Cats', 'smarter')
    # matchObj.groups(2): <class 'tuple'> ('Cats', 'smarter')

    print()
    print(len(line))  # 26
    print(matchObj.start(), line[matchObj.start()])  # 0 C
    print(matchObj.end(), line[matchObj.end()-1])  # 26 s
else:
    print("No match!!")

line2 = 'www.baidu.www.com'
match2 = re.match('www', line2)
print(type(match2), match2)  # <class '_sre.SRE_Match'> <_sre.SRE_Match object; span=(0, 3), match='www'>

search = re.search('www', line2)
print(type(search), search)  # <class '_sre.SRE_Match'> <_sre.SRE_Match object; span=(0, 3), match='www'>
